home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / MAINTEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  8.1 KB  |  258 lines

  1.  
  2. /* 
  3.  * texture map demo scaffolding
  4.  *
  5.  * FUNCTION:
  6.  * Most of this code is required to set up OpenGL and GLUT
  7.  *
  8.  * HISTORY:
  9.  * Written by Linas Vepstas, March 1995
  10.  */
  11.  
  12. /* required include files */
  13. #include <stdlib.h>
  14. #include <GL/glut.h>
  15. #include <GL/tube.h>
  16. #include "texture.h"
  17.  
  18. /*  most recent mouse postion */
  19. float lastx = 100.0;
  20. float lasty = 100.0;
  21.  
  22. extern void InitStuff (void);
  23. extern void DrawStuff (void);
  24.  
  25. /* get notified of mouse motions */
  26. void MouseMotion (int x, int y)
  27. {
  28.    lastx = x;
  29.    lasty = y;
  30.    glutPostRedisplay ();
  31. }
  32.  
  33. void TextureStyle (int msg) 
  34. {
  35.    int mode = 0;
  36.  
  37.    switch (msg) {
  38.       case 301:
  39.          glDisable (GL_TEXTURE_2D);
  40.          break;
  41.       case 302:
  42.          glEnable (GL_TEXTURE_2D);
  43.          break;
  44.  
  45.       case 501:
  46.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_FLAT;
  47.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  48.          glScalef (0.1, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  49.          gleTextureMode (mode);
  50.          break;
  51.       case 502:
  52.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_NORMAL_FLAT;
  53.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  54.          glScalef (0.1, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  55.          gleTextureMode (mode);
  56.          break;
  57.       case 503:
  58.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_CYL;
  59.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  60.          glScalef (1.0, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  61.          gleTextureMode (mode);
  62.          break;
  63.       case 504:
  64.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_NORMAL_CYL;
  65.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  66.          glScalef (1.0, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  67.          gleTextureMode (mode);
  68.          break;
  69.       case 505:
  70.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_SPH;
  71.          glMatrixMode (GL_TEXTURE); glLoadIdentity (); 
  72.          glMatrixMode (GL_MODELVIEW);
  73.          gleTextureMode (mode);
  74.          break;
  75.       case 506:
  76.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_NORMAL_SPH;
  77.          glMatrixMode (GL_TEXTURE); glLoadIdentity (); 
  78.          glMatrixMode (GL_MODELVIEW);
  79.          gleTextureMode (mode);
  80.          break;
  81.  
  82.       case 507:
  83.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
  84.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  85.          glScalef (0.1, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  86.          gleTextureMode (mode);
  87.          break;
  88.       case 508:
  89.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_NORMAL_MODEL_FLAT;
  90.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  91.          glScalef (0.1, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  92.          gleTextureMode (mode);
  93.          break;
  94.       case 509:
  95.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_CYL;
  96.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  97.          glScalef (1.0, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  98.          gleTextureMode (mode);
  99.          break;
  100.       case 510:
  101.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_NORMAL_MODEL_CYL;
  102.          glMatrixMode (GL_TEXTURE); glLoadIdentity ();
  103.          glScalef (1.0, 0.1, 1.0); glMatrixMode (GL_MODELVIEW);
  104.          gleTextureMode (mode);
  105.          break;
  106.       case 511:
  107.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_SPH;
  108.          glMatrixMode (GL_TEXTURE); glLoadIdentity (); 
  109.          glMatrixMode (GL_MODELVIEW);
  110.          gleTextureMode (mode);
  111.          break;
  112.       case 512:
  113.          mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_NORMAL_MODEL_SPH;
  114.          glMatrixMode (GL_TEXTURE); glLoadIdentity (); 
  115.          glMatrixMode (GL_MODELVIEW);
  116.          gleTextureMode (mode);
  117.          break;
  118.  
  119.       case 701:
  120.          current_texture = check_texture;
  121.          gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 
  122.                       current_texture -> size,
  123.                       current_texture -> size,
  124.                       GL_RGB, GL_UNSIGNED_BYTE, 
  125.                       (void *) (current_texture->pixmap));
  126.          break;
  127.       case 702:
  128.          current_texture = barberpole_texture;
  129.          gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 
  130.                       current_texture -> size,
  131.                       current_texture -> size,
  132.                       GL_RGB, GL_UNSIGNED_BYTE, 
  133.                       (void *) (current_texture->pixmap));
  134.          break;
  135.       case 703:
  136.          current_texture = wild_tooth_texture;
  137.          gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 
  138.                       current_texture -> size,
  139.                       current_texture -> size,
  140.                       GL_RGB, GL_UNSIGNED_BYTE, 
  141.                       (void *) (current_texture->pixmap));
  142.          break;
  143.       case 704:
  144.          current_texture = planet_texture;
  145.          gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 
  146.                       current_texture -> size,
  147.                       current_texture -> size,
  148.                       GL_RGB, GL_UNSIGNED_BYTE, 
  149.                       (void *) (current_texture->pixmap));
  150.          break;
  151.  
  152.       case 99:
  153.          exit (0);
  154.       default:
  155.          break;
  156.    }
  157.    glutPostRedisplay();
  158. }
  159.  
  160. /* set up a light */
  161. GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
  162. GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
  163.  
  164. GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
  165. GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
  166.  
  167. int
  168. main (int argc, char * argv[]) {
  169.  
  170.    /* initialize glut */
  171.    glutInit (&argc, argv);
  172.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  173.    glutCreateWindow ("texture");
  174.    glutDisplayFunc (DrawStuff);
  175.    glutMotionFunc (MouseMotion);
  176.  
  177.    /* create popup menu */
  178.    glutCreateMenu (TextureStyle);
  179.    glutAddMenuEntry ("Texture Off", 301);
  180.    glutAddMenuEntry ("Texture On", 302);
  181.    glutAddMenuEntry ("--------------", 9999);
  182.    glutAddMenuEntry ("Vertex Flat", 501);
  183.    glutAddMenuEntry ("Normal Flat", 502);
  184.    glutAddMenuEntry ("Vertex Cylinder", 503);
  185.    glutAddMenuEntry ("Normal Cylinder", 504);
  186.    glutAddMenuEntry ("Vertex Sphere", 505);
  187.    glutAddMenuEntry ("Normal Sphere", 506);
  188.    glutAddMenuEntry ("--------------", 9999);
  189.    glutAddMenuEntry ("Model Vertex Flat", 507);
  190.    glutAddMenuEntry ("Model Normal Flat", 508);
  191.    glutAddMenuEntry ("Model Vertex Cylinder", 509);
  192.    glutAddMenuEntry ("Model Normal Cylinder", 510);
  193.    glutAddMenuEntry ("Model Vertex Sphere", 511);
  194.    glutAddMenuEntry ("Model Normal Sphere", 512);
  195.    glutAddMenuEntry ("--------------", 9999);
  196.    glutAddMenuEntry ("Check Texture", 701);
  197.    glutAddMenuEntry ("Barberpole Texture", 702);
  198.    glutAddMenuEntry ("Wild Tooth Texture", 703);
  199.    glutAddMenuEntry ("Molten Lava Texture", 704);
  200.    glutAddMenuEntry ("--------------", 9999);
  201.    glutAddMenuEntry ("Exit", 99);
  202.    glutAttachMenu (GLUT_MIDDLE_BUTTON);
  203.  
  204.    /* initialize GL */
  205.    glClearDepth (1.0);
  206.    glEnable (GL_DEPTH_TEST);
  207.    glClearColor (0.0, 0.0, 0.0, 0.0);
  208.    glShadeModel (GL_SMOOTH);
  209.  
  210.    glMatrixMode (GL_PROJECTION);
  211.    /* roughly, measured in centimeters */
  212.    glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0);
  213.    glMatrixMode(GL_MODELVIEW);
  214.  
  215.    /* initialize lighting */
  216.    glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
  217.    glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
  218.    glEnable (GL_LIGHT0);
  219.    glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
  220.    glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
  221.    glEnable (GL_LIGHT1);
  222.    glEnable (GL_LIGHTING);
  223.    glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
  224.    glEnable (GL_COLOR_MATERIAL);
  225.    glColor3f (0.8, 0.3, 0.6);
  226.  
  227.    /* initialize and enable texturing */
  228.    setup_textures ();
  229.    gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 
  230.                       current_texture -> size,
  231.                       current_texture -> size,
  232.                       GL_RGB, GL_UNSIGNED_BYTE, 
  233.                       (void *) (current_texture->pixmap));
  234.  
  235.    glMatrixMode (GL_TEXTURE);
  236.    glLoadIdentity ();
  237.    glScalef (1.0, 0.1, 1.0);
  238.    glMatrixMode (GL_MODELVIEW);
  239.  
  240.    glEnable (GL_TEXTURE_2D);
  241. /*
  242.    some stuff to play with ....
  243.    glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  244.    glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  245.    glEnable (GL_TEXTURE_GEN_S);
  246.    glEnable (GL_TEXTURE_GEN_T);
  247. */
  248.  
  249.    gleTextureMode (GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_CYL);
  250.  
  251.    InitStuff ();
  252.  
  253.    glutMainLoop ();
  254.    return 0;             /* ANSI C requires main to return int. */
  255. }
  256.  
  257. /* -------------------- end of file -------------------- */
  258.